import { fileURLToPath, URL } from 'node:url'
import path from 'path'
import { defineConfig, loadEnv } from 'vite'
import tailwindcss from '@tailwindcss/vite'
import vue from '@vitejs/plugin-vue'
// import vueDevTools from 'vite-plugin-vue-devtools'
import Components from 'unplugin-vue-components/vite'
import { AntDesignVueResolver } from 'unplugin-vue-components/resolvers'
import AppLoading from 'vite-plugin-app-loading'

import { buildVersionPlugin } from './scripts/version'
// 配置svg图标
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
// https://vite.dev/config/
export default defineConfig(({ mode }) => {
    // 加载环境变量
    const env = loadEnv(mode, process.cwd(), '')
    const { VITE_API_BASE_URL, VITE_BASE_URL, VITE_PORT } = env

    return {
        plugins: [
            buildVersionPlugin(),
            vue(),
            tailwindcss(),
            AppLoading('loading.html'),
            Components({
                resolvers: [
                    AntDesignVueResolver({
                        importStyle: false, // css in js
                    }),
                ],
            }),
            createSvgIconsPlugin({
                // 指定需要缓存的图标文件夹
                iconDirs: [path.resolve(process.cwd(), 'src/assets/icons')],
                // 指定symbolId格式
                symbolId: 'icon-[dir]-[name]',
                /**
                 * 自定义插入位置
                 * @default: body-last
                 */
                inject: 'body-last',
                /**
                 * custom dom id
                 * @default: __svg__icons__dom__
                 */
                customDomId: '__svg__icons__dom__',
            }),
        ],
        resolve: {
            alias: {
                '@': fileURLToPath(new URL('./src', import.meta.url)),
            },
        },

        server: {
            proxy: {
                [VITE_API_BASE_URL]: {
                    target: VITE_BASE_URL,
                    changeOrigin: true,
                    rewrite: (path) => path.replace(VITE_API_BASE_URL, ''),
                },
            },
            port: Number(VITE_PORT),
        },
    }
})
